import nyNetwork from "@/api/nyBaseNetwork";
import DZModelPage from "@/model/DZModelPage";
import dateUtil from "@/utils/DZDateUtil";
import DZModelBatch from "../model/DZModelBatch";
import DZModelCoupon from "../model/DZModelCoupon";

class DZServiceCoupon {
    m_page = new DZModelPage(10);

    list_batch: Array<DZModelBatch> = [];

    m_batch = new DZModelBatch();

    m_batch_screen = {
        name: null,
        goods: null
    }

    list_coupon: Array<DZModelCoupon> = [];

    net_list_batch() {
        var arr_data = [];
        nyNetwork.net("批次列表", { ...this.m_batch_screen, ...this.m_page }, data => {
            this.m_page.total = data.total;
            for (const item of data.list) {
                var model = new DZModelBatch();
                model.id = item.iid;
                model.name = item.sname ?? '';
                model.goodsName = item.sgoodsName ?? '';
                model.termValidity = dateUtil.formatConversionTotal(item.ltermValidity);
                model.num = item.inum ?? 0;
                model.useNum = item.iuseNum ?? 0;
                model.surplusNum = model.num - model.useNum;
                arr_data.push(model);
            }
            this.list_batch = arr_data;
        })
    }

    net_add_batch(success: () => void) {
        var params = {
            name: this.m_batch.name,
            goods: this.m_batch.goodesId,
            days: this.m_batch.termValidityDay,
            num: this.m_batch.num
        }
        nyNetwork.net("创建批次", params, data => {
            success();
        })
    }

    net_list_coupon(id, success: () => void) {
        var arr_data = [];
        nyNetwork.net("券码列表", { id: id }, data => {
            for (const item of data) {
                var model = new DZModelCoupon();
                model.info = item.sinfo ?? '';
                model.status = item.istatus ?? 0;
                model.phone = item.iuserPhone ?? '';
                arr_data.push(model);
            }
            this.list_coupon = arr_data;
            success();
        })
    }

    net_export_coupon(name, id) {
        nyNetwork.downFile("导出券码列表", { id: id }, name + ".xlsx")
    }
}
export default DZServiceCoupon;